package com.donger.business.cms.controller;



import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import lombok.AllArgsConstructor;
import com.donger.business.cms.entity.CmsCategory;
import com.donger.business.cms.service.CmsCategoryService;

import java.util.List;
import java.util.stream.Collectors;


/**
 * cms分类(CmsCategory)表控制层
 *
 * @author somliy
 * @since 2020-05-07 19:03:34
 */
@RestController
@RequestMapping("/cmsCategory")
@AllArgsConstructor
@Slf4j
public class CmsCategoryController {
    /**
     * 服务对象
     */
    private final CmsCategoryService cmsCategoryService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param cmsCategory 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    public Result listPageAll(Page<CmsCategory> page, CmsCategory cmsCategory) {
        return Res.ok(cmsCategoryService.page(page, new QueryWrapper<>(cmsCategory)));
    }
    
     /**
     * 查询所有数据
     *
     * @param cmsCategory 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public Result listAll(CmsCategory cmsCategory) {
        return Res.ok(cmsCategoryService.list(new QueryWrapper<>(cmsCategory)));
    }
    
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public Result selectOne(@PathVariable("id") Long id) {
        return Res.ok(cmsCategoryService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param cmsCategory 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    public Result insert(@RequestBody CmsCategory cmsCategory) {
        return Res.ok(cmsCategoryService.save(cmsCategory));
    }

    /**
     * 修改数据
     *
     * @param cmsCategory 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public Result update(@RequestBody CmsCategory cmsCategory) {
        return Res.ok(cmsCategoryService.updateById(cmsCategory));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result delete(@RequestParam("id") Long id) {
        return Res.ok(cmsCategoryService.removeById(id));
    }

    /**
     * 查询所有
     */
    @GetMapping("tree")
    public Result<List<CmsCategory>> listResult(){
        List<CmsCategory> categoryList = cmsCategoryService.list();
        return Res.ok(recursiveBuildTree(categoryList,0L));
    }

    /**
     * 选择上级菜单
     */
    @GetMapping("/select")
    public Result select() {
        List<CmsCategory> categoryList = cmsCategoryService.list();
        CmsCategory cmsCategory = new CmsCategory();
        cmsCategory.setCategoryId(0L);
        cmsCategory.setName("一级类别");
        cmsCategory.setParentId(-1L);
        categoryList.add(cmsCategory);
        return Res.ok(recursiveBuildTree(categoryList, -1L));
    }

    /**
     * 递归建树
     * 不包含根节点
     */
    private List<CmsCategory> recursiveBuildTree(List<CmsCategory> cmsCategoryList, Long parentId){
        List<CmsCategory> childList = cmsCategoryList.stream().filter(o->o.getParentId().equals(parentId)).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(childList)){
            return null;
        }
        for (CmsCategory cmsCategory : childList){
            cmsCategory.setChildren(recursiveBuildTree(cmsCategoryList,cmsCategory.getCategoryId()));
        }
        return childList;
    }

}